package org.openkoala.koala.popup.generatecode;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;
import org.openkoala.koala.deploy.db2entity.DB2EntityUtil;

/**
 * 主界面
 * @author lingen
 *
 */
public class GenerateEntityDialog extends Dialog {

	private String projectPath;
	
	private GenerateEntityFromDatabaseAction action;
	
	private Tree tableTree;
	private Combo exportPositionText;
	private Text packageText;
	
	private DB2EntityUtil db2EntityUtil;
	
	private Shell shell;
	
	private List<String> bizModules;
	
	protected GenerateEntityDialog(Shell parentShell, String projectPath, GenerateEntityFromDatabaseAction action,DB2EntityUtil db2EntityUtil,List<String> bizModules) {
		super(parentShell);
		this.shell = parentShell;
		this.projectPath = projectPath;
		this.action = action;
		this.db2EntityUtil = db2EntityUtil;
		this.bizModules = bizModules;
	}

	@Override
	protected Control createDialogArea(Composite parent) {
		Composite composite = new Composite(parent, SWT.NONE);
		initializeDialogUnits(composite);

		GridData mainGridData = new GridData(500, 500);
		composite.setLayoutData(mainGridData);

		GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 1;
		composite.setLayout(gridLayout);
		
		createExportPositionComposite(composite);
		createTableSelecter(composite);

		return composite;
	}

	
	
	private void createExportPositionComposite(Composite parent) {
		Composite composite = new Composite(parent, SWT.NONE);
		composite.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

		GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 2;
		composite.setLayout(gridLayout);
		
		GridData textGridData = new GridData(GridData.FILL_HORIZONTAL);
		textGridData.heightHint = 20;

		Label projectLabel = new Label(composite, SWT.NONE);
		projectLabel.setText("项目选择");
		
		exportPositionText = new Combo(composite, SWT.BORDER);
		for(String bizModule:this.bizModules){
			exportPositionText.add(bizModule);
		}
		exportPositionText.select(0);
		exportPositionText.setLayoutData(textGridData);
		
		Label label = new Label(composite, SWT.NONE);
		label.setText("包路径");
		
		packageText = new Text(composite, SWT.BORDER);
		packageText.setLayoutData(textGridData);
	}

	public void refreshTableTree() {
		try {
			tableTree.removeAll();
			for (String table : db2EntityUtil.getTables()) {
				TreeItem dataBaseNode = new TreeItem(tableTree, SWT.NONE);
				dataBaseNode.setText(table);
			}
			db2EntityUtil.closeConn();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		for (TreeItem treeItem : tableTree.getItems()) {
			treeItem.setExpanded(true);
		}
	}

	private void createTableSelecter(Composite parent) {
		Composite composite = new Composite(parent, SWT.NONE);
		composite.setLayoutData(new GridData(GridData.FILL_BOTH));
		
		GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 2;
		composite.setLayout(gridLayout);
		
		try {
			createTableTree(composite);
			refreshTableTree();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	

	private void createTableTree(Composite parent) throws Exception {
		GridData treeGridData = new GridData(GridData.FILL_BOTH);
		
		tableTree = new Tree(parent, SWT.MULTI | SWT.BORDER | SWT.CHECK);
		treeGridData.minimumWidth = 280;
		tableTree.setLayoutData(treeGridData);
		
		Group buttonGroup = new Group(parent,SWT.BORDER);
		GridLayout layout = new GridLayout();
		layout.numColumns = 1;
		buttonGroup.setLayout(layout);
		buttonGroup.setLayoutData(new GridData(GridData.FILL_BOTH));
		
		Button selectAll = new Button(buttonGroup,SWT.PUSH);
		selectAll.setText("Select All");
		selectAll.setLayoutData(new GridData());
		selectAll.addSelectionListener(new SelectionListener(){
			@Override
			public void widgetDefaultSelected(SelectionEvent arg0) {
			}

			@Override
			public void widgetSelected(SelectionEvent arg0) {
				for (TreeItem treeItem : tableTree.getItems()) {
					treeItem.setChecked(true);
				}		
			}});
		
		Button deSelectAll = new Button(buttonGroup,SWT.PUSH);
		deSelectAll.setText("Deselect All");
		deSelectAll.setLayoutData(new GridData());
		deSelectAll.addSelectionListener(new SelectionListener(){
			@Override
			public void widgetDefaultSelected(SelectionEvent arg0) {
			}

			@Override
			public void widgetSelected(SelectionEvent arg0) {
				for (TreeItem treeItem : tableTree.getItems()) {
					treeItem.setChecked(false);
				}
			}});
	}
	
	@Override
	protected void okPressed() {
		
		if(exportPositionText.getText()==null || exportPositionText.getText().trim().equals("")){
			MessageDialog.openError(shell,
					"ERROR", "请选择目标项目");
			return;
		}
		
		if(packageText.getText()==null || packageText.getText().trim().equals("")){
			MessageDialog.openError(shell,
					"ERROR", "请输入实体的包路径");
			return;
		}
		
		String destProjectPath = projectPath +"/"+ exportPositionText.getText().replace("/", "\\");
		
		List<String> tableNames = new ArrayList<String>();
		for (TreeItem treeItem : tableTree.getItems()) {
			if (treeItem.getChecked()) {
				tableNames.add(treeItem.getText());
			}
		}		

		if(tableNames.size()==0){
			MessageDialog.openError(shell,
					"ERROR", "请至少选择一个表");
			return;
		}
		action.generateEntity(db2EntityUtil, tableNames, destProjectPath, packageText.getText());
		super.okPressed();
	}
}
